package com.jiantong.user.impl.service;

import com.google.common.base.Throwables;
import com.jiantong.user.impl.dao.RolesWebDao;
import com.jiantong.user.model.RolesWeb;
import com.jiantong.user.service.RolesWebReadService;
import com.jiantong.user.service.RolesWebWriteService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

/**
 * Created by niufei on 2017/4/5.
 */
@Slf4j
@Service
@RpcProvider
public class RolesWebWriteServiceImpl implements RolesWebWriteService {

    private final RolesWebDao rolesWebDao;

    @Autowired
    public RolesWebWriteServiceImpl(RolesWebDao rolesWebDao) {
        this.rolesWebDao = rolesWebDao;
    }


    @Override
    public Response<Long> create(RolesWeb rolesWeb) {
        try {
            rolesWebDao.create(rolesWeb);
        return Response.ok(rolesWeb.getId());
        } catch (DuplicateKeyException e) {
            log.error("failed to create {}, cause:{}", Throwables.getStackTraceAsString(e));
            return Response.fail("user.loginId.duplicate");
        } catch (Exception e) {
            log.error("failed to create {}, cause:{}", Throwables.getStackTraceAsString(e));
            return Response.fail("user.create.fail");
        }
    }

    @Override
    public Response<Boolean> update(RolesWeb rolesWeb) {
        try {
        return Response.ok(rolesWebDao.update(rolesWeb));
        } catch (DuplicateKeyException e) {
            log.error("failed to create {}, cause:{}", Throwables.getStackTraceAsString(e));
            return Response.fail("user.loginId.duplicate");
        } catch (Exception e) {
            log.error("failed to create {}, cause:{}", Throwables.getStackTraceAsString(e));
            return Response.fail("user.create.fail");
        }
    }
}
